<template>
  <div class="ChildPage TablePage" v-loading="loading">
    <vb-page ref="page" @onTableSelection="doSelect">
      <template #headerSearch-btn-append="{ searchForm }">
        <el-button size="small" type="success"  icon="el-icon-share" @click="uploadKingdee(this.selection)">批量上传金蝶</el-button>
        <el-button size="small" icon="el-icon-upload2" type="warning" @click="$refs.upload_dialog.show()">导入excel</el-button>
        <el-button size="small" icon="el-icon-upload2" type="warning" @click="$refs.upload_excle.show()">导入扣点excel</el-button>
        <el-button size="small" icon="el-icon-upload2" type="warning" @click="$refs.upload_guangnian.show()">导入光年excel</el-button>

      </template>

      <template #table-handle-prepend="scope">
        <el-button size="mini" type="text" icon="el-icon-edit" @click="gotoDetail(scope.row.billNo)">查看</el-button>
      </template>
      <template #table-isUpload="scope">
        <el-tag :type="scope && scope.row && scope.row.isUpload === 'yes'? 'success': 'info'">{{scope && scope.row && scope.row.isUpload === 'yes'? '已上传': '未上传'}}</el-tag>
      </template>
      <template #table-orderUpLoad="scope">
        <el-tag :type="scope && scope.row && scope.row.orderUpLoad === 'yes'? 'success': 'info'">{{scope && scope.row && scope.row.orderUpLoad === 'yes'? '已上传': '未上传'}}</el-tag>
      </template>

    </vb-page>
  </div>
  <!--  正常传单-->
  <upload-dialog ref="upload_dialog" upload-method="upload_yexiang_sale_bill" @success="uploadFinish"></upload-dialog>
  <!--  扣点传单-->
  <upload-excle ref="upload_excle" upload-method="upload_yexiang_sale_bill_points" @success="uploadFinish"></upload-excle>

  <!--  光年传网店订单号-->
  <upload-excle ref="upload_guangnian" upload-method="upload_guangnian_online" @success="uploadFinish"></upload-excle>
  <selectRowStockIn ref="selectRowStockIn" @success=""></selectRowStockIn>
</template>

<script>
import uploadDialog from '../components/uploadDialog.vue'
import uploadExcle from "../components/uploadExcle.vue";
import uploadGuangnian from "../components/uploadGuangnian.vue";
import selectRowStockIn from '../components/selectRowStockInDialog.vue'

export default {
  data() {
    return {
      loading:false,
      selection: null,
      selectionLength:0,
      stockLoading: false,
      reloadStock: false,
    }
  },
  components: {
    uploadDialog,
    selectRowStockIn,
    uploadExcle,
    uploadGuangnian
  },
  methods: {
    async getStockReloadResult(showMsg) {
      this.stockLoading = true
      try {
        let res = await this.callApi("get_stock_reload_result")
        if (!res.res) return
        this.reloadStock = res.reloadStock
        this.reloadStockRes = res.reloadStockRes
        this.reloadStockMsg = res.reloadStockMsg
        this.reloadStockStartTime = res.reloadStockStartTime
        this.reloadStockEndTime = res.reloadStockEndTime
        if (showMsg) {
          if (this.reloadStock) {
            this.$message.info('数据正在上传中')
          } else {
            if (this.reloadStockRes) {
              this.reload()
              this.$message.success('已同步完成, 请刷新最新结果')
            } else {
              this.$message.error('同步失败，' + this.reloadStockMsg)
            }
          }
        }
      } finally {
        this.stockLoading = false
      }
    },

    async uploadKingdee(billNoList){
      if (!this.selection || this.selection === 0){
        this.$message.error('没有选择行')
        return
      }
      const arrBillNo = [];
      for (let i = 0;i<billNoList.length;i++){
        arrBillNo.push(billNoList[i].tradeNo)
      }

      try {
        this.$message.warning('正在上传')
        let res = await this.callApi('upload_kingdee_sale',{billNoList:arrBillNo})
        if(!res.res){
          return
        }
        this.$message.success('上传成功')
        this.$refs.page.reload()
      }finally {
        this.loading = false
      }
    },
    doSelect(data) {
      this.selection = data
      this.selectionLength = this.selection.length;
      this.selectCount=0
      for (var i=0;i<this.selectionLength;i++){
        this.selectCount+=data[i].count
      }
      this.selectCount = parseFloat(this.selectCount).toFixed(2)
      console.log(this.selection)
    }
  }
}
</script>

<style scoped>

</style>
